//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "IntegralEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phIntegralEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppIntegralEngine = new boost::shared_ptr<QuantLib::IntegralEngine> (new QuantLib::IntegralEngine ( _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (boost::shared_ptr<QuantLib::IntegralEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralEngine = NULL;
#endif
	_ppIntegralEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (QuantLib::IntegralEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIntegralEngine = NULL;
#endif
	_ppIntegralEngine = new boost::shared_ptr<QuantLib::IntegralEngine> (&childNative);
    
    _IntegralEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (CIntegralEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIntegralEngine = new boost::shared_ptr<QuantLib::IntegralEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralEngine = NULL;
#endif
	if (!t->IsSubclassOf(CIntegralEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (QuantLib::Handle<QuantLib::IntegralEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegralEngine = &childNative;
	_ppIntegralEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralEngine>>(childNative.currentLink());
    
    _IntegralEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (QuantLib::Handle<QuantLib::IntegralEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegralEngine = &childNative;
	_ppIntegralEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::CIntegralEngine (QuantLib::IntegralEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegralEngine = NULL;
#endif
	_ppIntegralEngine = new boost::shared_ptr<QuantLib::IntegralEngine> (new QuantLib::IntegralEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::~CIntegralEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIntegralEngine != NULL)
    {
	    delete _ppIntegralEngine;
        _ppIntegralEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::!CIntegralEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIntegralEngine != NULL)
    {
	    delete _ppIntegralEngine;
    }
}
QuantLib::IntegralEngine& Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::GetReference ()
{
    if (_ppIntegralEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppIntegralEngine;
}
boost::shared_ptr<QuantLib::IntegralEngine>& Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::GetShared ()
{
    if (_ppIntegralEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppIntegralEngine;
}
QuantLib::IntegralEngine* Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::GetPointer ()
{
    if (_ppIntegralEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppIntegralEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IntegralEngine>& Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::GetHandle ()
{
	if (_phIntegralEngine == NULL)
	{
		_phIntegralEngine = new Handle<QuantLib::IntegralEngine> (*_ppIntegralEngine);
	}
	return *_phIntegralEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::HasNative () 
{
	return (_ppIntegralEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IIntegralEngine^ Cephei::QL::Pricingengines::Vanilla::CIntegralEngine::Calculate::get ()
{
    try
    {
    	(*_ppIntegralEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IIntegralEngine^ Cephei::QL::Pricingengines::Vanilla::CIntegralEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return REFNEW CIntegralEngine ( process);
}
